package com.thinkgsp.webapp.controller.base;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.thinkgsp.jxc.model.Emp;
import com.thinkgsp.jxc.service.EmpService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExtReturn;
@Controller
@RequestMapping(value = "/base")
public class EmployeeController  extends BaseController{
	
	private EmpService empService;
	
	
	
	@Autowired
	public void setEmpService(EmpService empService){
		this.empService=empService;
	}
	
	
	@RequestMapping("/emplist.do")
    @ResponseBody
    public List<Emp> getEmps(HttpServletRequest request,
			HttpServletResponse response){
		String deptIdStr=request.getParameter("deptId");
		if(deptIdStr!=null){
			Integer deptId=Integer.decode(deptIdStr);
			return this.empService.getEmpByDeptId(deptId);
		}
		return empService.getAllEmp();
	}
	@RequestMapping(value = "/addemp.do")
	@ResponseBody
	public Object add(Model model ,@RequestBody Emp emp) {
		
		try {
			this.empService.saveEmp(emp);
			return new ExtReturn(false,"增加职员信息成功");
		} catch (Exception e) {
			return new ExtReturn(false,"增加职员信息出错",e.getMessage());
		}
	}
	
	@RequestMapping(value = "/updemp.do")
	@ResponseBody
	public Object upd(Model model ,@RequestBody Emp emp) {
		try {
			this.empService.saveEmp(emp);
			return new ExtReturn(false,"修改职员信息成功");
		} catch (Exception e) {
			return new ExtReturn(false,"修改职员信息出错",e.getMessage());
		}
	}
	@RequestMapping(value = "/delemp.do")
	@ResponseBody
	public Object del(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String pam=request.getParameter("ids");
			String[] ids=pam.split(",");
			for (String id : ids) {
				this.empService.deleteEmp(Integer.decode(id));
			}
			return new ExtReturn(false,"删除职员信息成功");
		} catch (Exception e) {
			return new ExtReturn(false,"删除职员信息出错",e.getMessage());
		}
	}
}
